<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */
class TTable extends TWidget{
    
    public $Headers = array();
    public $Columns = 3;
    public $Rows = 1;
    public $Items = array();
    public $WidthCell = array();
    public $HeightRows = 25;
    public $HeaderAlign = array();
    public $RowsAlign = array();
    public $ShowFooter = true;
    public $IdRows = array();
    public $IdCell = '';
    public $Pagination = '';
    
    //el filtro quererusarlo
    
    public $FilterField = array();
    
    public function __construct($name = __CLASS__) {
        parent::__construct($name);
        $this->Width = '400px';
        
    }
    
    public function show(){
        
        //cargamos las opciones por defecto
        
        if (!array_key_exists('style', $this->OptionsAditional)){
            $this->OptionsAditional['style'] = array('width:'.$this->Width);
        }
        else{
            $this->OptionsAditional['style'][] = 'width:'.$this->Width;
        }
        
        //si esta marcada como visible 
        
        if ($this->Visible == true){
            
            
            $table = '<table id="'.$this->Id.'" class="grid" '.$this->getOptionsAditional().'>';
            
            //comenzamos a cargar las cabeceras
            
            if (count($this->Headers) > 0){ //verificamos que el usuario indico cabeceras si no es asi las obviamos
            
                $HeaderCount = 0;
                $table .= '<tr>';

                while ($HeaderCount < $this->Columns ){
                    if (isset($this->WidthCell[$HeaderCount])){
                        $style_width = "width:".$this->WidthCell[$HeaderCount]."px; ";
                    }
                    else{
                        $style_width = "";
                    }
                    
                    if (isset($this->HeaderAlign[$HeaderCount])){
                        $align_text = 'text-align:'.$this->HeaderAlign.';';
                    }
                    else{
                        $align_text = '';
                    }
                    
                    $table .= '<th class="grid_th" style="height:'.$this->HeightRows.'px; '.$style_width.' '.$align_text.'">'.$this->Headers[$HeaderCount].'</th>';
                    $HeaderCount++;
                    
                }
                
                $table .= '</tr>';
            
            }
            
            //comenzamos con la carga de valores por defecto
            
            if (count($this->Items) > 0){
                
                //validamos que los registros vengan en un array
                
                if (!isset($this->Items[0]) or !is_array($this->Items[0])){
                    throw new Exception("Error los registros deben de ser un array indexado");
                }
                elseif (count($this->Items[0]) < count($this->Headers)){
                    throw new Exception("las cantidad de columnas en los registros es menor a las cantidad la cantidad de la cabecera");
                }
                else{
                    
                    //iniciamos la carga de los registros indicados
                    
                    $RowsCount = 0;
                    
                    while ($RowsCount < ($this->Rows)){
                        if (array_key_exists($RowsCount, $this->Items)){
                            
                            if (isset($this->IdRows[$RowsCount])){
                                $id_row = 'id="'.$this->IdRows[$RowsCount].'"';
                            }
                            else{
                                $id_row = '';
                            }
                            
                            $table .= "<tr ".$id_row.">";

                                $countCol = 0;

                                foreach ($this->Items[$RowsCount] as $key=>$cell){
                                    if ($countCol < count($this->Headers)){
                                        
                                        if (isset($this->RowsAlign[$countCol])){
                                            $rows_align_text = 'text-align:'.$this->RowsAlign[$countCol].';';
                                        }
                                        else{
                                            $rows_align_text = '';
                                        }
                                        
                                        $table .= '<td id="grid-row" class="grid_td" style="height:'.$this->HeightRows.'px; '.$rows_align_text.'">'.$cell.'</td>';
                                        $countCol++;
                                    }
                                }

                            $table .= "</tr>";
                            
                        }
                        $RowsCount++;
                    }
                }
            }
            if ($this->ShowFooter ==true){
                if (is_object($this->Pagination)){
                    $pagination = $this->Pagination->renderPagination();
                }
                else{
                    $pagination = '';
                }
                
                $table .= '<tr><td id="grid-footer" colspan="'.$this->Columns.'">&nbsp;&nbsp;&nbsp;'.$pagination.'&nbsp;</td></tr>';
                
            }
           
            $table .= '</table>';    
            return $table;
        }
        
    }
}
?>
